<?php

namespace App\Controller\InsideService;

use App\Constants\QmGame;
use App\Service\ToolService;

class GameConfigInside extends BaseInside {

    /**
     * 获取配置列表
     * @param string $size
     * @param string $game_hash
     * @param string $keywords
     * @param int $page
     * @return array
     */
    public function index(string $size = '', string $game_hash = '', string $keywords = '', int $page = 1): array {
        return $this->getInfo('GameConfig/index', [
            'page'      => $page,
            'size'      => $size,
            'game_hash' => $game_hash,
            'keywords'  => $keywords
        ]);
    }

    /**
     * 编辑配置列表
     * @param array $data
     * @return array
     */
    public function edit(array $data): array {
        return $this->postInfo('GameConfig/edit', $data);
    }

    /**
     * 提交配置表唯一标识
     * @param $data
     * @return mixed
     */
    public function confirmConf($data): mixed {
        return $this->postInfo('GameConfig/confirmConf', [
            'unique'    => $data['unique'],
            'game_hash' => $data['game_hash'],
            'id'        => $data['id']
        ]);
    }

    /**
     * 获取配置数据
     * @param $key
     * @param $gameHash
     * @param string $newIndex
     * @return mixed
     */
    public function getConfInfoByKey($key, $gameHash, string $newIndex = ''): mixed {
        $detail = '';

        $res = $this->getInfo('GameConfig/getConfInfoByKey', [
            'key'       => $key,
            'game_hash' => $gameHash
        ]);

        if (isset($res['data']) && !empty($res['data']['list'])) {
            $jsonFile = QmGame::CONFIG_JSON_PREFIX . $res['data']['list'] . '.json';
            if (file_exists($jsonFile)) {
                $detail = file_get_contents(QmGame::CONFIG_JSON_PREFIX . $res['data']['list'] . '.json');
            }
        }

        if ($detail) {
            $confDetail = json_decode($detail, true);
            if ($confDetail) {
                if ($newIndex) {
                    return ToolService::buildArrByNewKey($confDetail, $newIndex);
                }

                return $confDetail;
            }
        }

        return [];
    }

    /**
     * 删除配置表
     * @param $id
     * @param $game_hash
     * @return mixed
     */
    public function del($id, $game_hash): mixed {
        return $this->getInfo('GameConfig/del', [
            'id'        => $id,
            'game_hash' => $game_hash
        ]);
    }

    /**
     * 新增配置表
     * @param $data
     * @return mixed
     */
    public function add($data): mixed {
        return $this->postInfo('GameConfig/add', $data);
    }

    /**
     * 复制配置表
     * @param $game_hash_from
     * @param $game_hash_to
     * @return mixed
     */
    public function updateConf($game_hash_from, $game_hash_to): mixed {
        return $this->postInfo('GameConfig/updateConf', [
            'game_hash_from' => $game_hash_from,
            'game_hash_to'   => $game_hash_to
        ]);
    }
}